package slider_verify

import (
	"fmt"
	partner "gitee.com/binny_w/gin-partner"
	"gitee.com/binny_w/gin_starter/core"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"time"
)

func get(c *gin.Context) {
	ret, err := generate()
	if err != nil {
		_ = c.AbortWithError(555, err)
		return
	}
	id := uuid.New().String()
	key := fmt.Sprintf("slide_verify:%s", id)
	if _, err = core.RedisVerify.HMSet(key, map[string]any{
		"UA":   c.Request.UserAgent(),
		"IP":   c.ClientIP(),
		"X":    ret.Point.X,
		"Code": "",
	}); err != nil {
		_ = c.AbortWithError(555, err)
		return
	}
	_, _ = core.RedisVerify.Expire(key, time.Minute)
	partner.WriteJsonSuccess(c, gin.H{
		"id":  id,
		"bg":  ret.BackgroundImg,
		"blk": ret.BlockImg,
		"y":   ret.Point.Y,
	})
}
